package com.haiyou.dc.thread.test3;

public class TestThreadLocal02 {

//    private static ThreadLocal<Integer> threadLocal = new ThreadLocal(){
//        @Override
//        protected Object initialValue() {
//            return 20;
//        }
//    };

    private static ThreadLocal<Integer> threadLocal = ThreadLocal.withInitial(() -> 1);

    public static void main(String[] args) {

        System.out.println(Thread.currentThread().getName() + "------>" + threadLocal.get());
        threadLocal.set(300);
        System.out.println(Thread.currentThread().getName() + "------>" + threadLocal.get());

        for (int i = 0; i <5 ; i++) {
            new Thread(new MyRun()).start();
        }
    }

    static class MyRun implements Runnable {

        @Override
        public void run() {
            Integer left = threadLocal.get();
            System.out.println(Thread.currentThread().getName() + "得到------>" + left);
            threadLocal.set(left - 1);
            System.out.println(Thread.currentThread().getName() + "剩下------>" + threadLocal.get());
        }
    }

}
